package wiBase;

import java.util.ArrayList;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import wiEngine.WiSchedulerThreadPoolExecutor;

/**
 * abstract class for schedule events in simulation.
 *
 * @see WiEngine.class
 */
public abstract class WiScheduler {

    public static final boolean FLAG_DEBUG = false;
    
    public static final int STATE_PAUSE = 0;
    public static final int STATE_PLAY = 1;
    public static final int STATE_STOP = 2;
    public static int STATE_CURRENT = STATE_STOP;
    
    protected static double CURRENT_SPEED = 1;
    protected static TimeUnit CURRENT_TIME_UNIT = TimeUnit.NANOSECONDS;
    protected static long START_TIME = -1;
    protected static long FINISH_TIME = -1;
    protected static long BEFORE_TIME = 1000000;
    private long START_TIME_9E = 0;
    private long FINISH_TIME_9E = 0;
    private long CURRENT_TIME_9E = 0;
    protected static WiSchedulerThreadPoolExecutor mScheduler = null;

    /**
     * set current time simulation
     *
     * @param currentTime: nanosecond (1E9)
     * @synchronized
     */
    public synchronized void setCurrentTime9E(long currentTime) {
	CURRENT_TIME_9E = currentTime;
	if (FLAG_DEBUG) {
	    System.out.println("set 9E: " + CURRENT_TIME_9E);
	}
    }

    /**
     * @return current time in nanosecond
     */
    public long getCurrentTime9E() {
	return CURRENT_TIME_9E;
    }

    /**
     * @return start time in nanosecond
     */
    public long getStartTime9E() {
	return START_TIME_9E;
    }

    /**
     * set time start for simulation
     *
     * @param startTime9E: nanosecond
     */
    protected void setStartTime9E(long startTime9E) {
	START_TIME_9E = startTime9E;
    }

    /**
     * get end time for simulation
     *
     * @return finish Time in nanosecond
     */
    public long getFinishTime9E() {
	return FINISH_TIME_9E;
    }

    /**
     * set end time for simulation
     *
     * @param finishTime9E: nanosecond
     */
    protected void setFinishTime9E(long finishTime9E) {
	FINISH_TIME_9E = finishTime9E;
    }

    /**
     * @param traceFile: path of trace file
     */
    public abstract void setTraceFile(String traceFile);

    /**
     * @return array list of nodes
     */
    public abstract ArrayList<WiObject> getListNodes();

    /**
     * @return array list of packets
     */
    public abstract ArrayList<WiObject> getListPackets();
    // LOG
    private static TreeMap<Long, StringBuilder> LOG = new TreeMap<Long, StringBuilder>();

    public static void appendLog(String strApp) {
	LOG.put(System.currentTimeMillis(), new StringBuilder(strApp));
    }

    public static void resetLog() {
	LOG = new TreeMap<Long, StringBuilder>();
    }

    public static TreeMap<Long, StringBuilder> getLog() {
	return LOG;
    }
}
